package objects;

import OBJ_Loader.AnimatedObject;
import OBJ_Loader.AnimatedObjectBase;
import graphic.Camera;
import graphic.Color;
import graphic.Graphic;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;


/**
 *
 * @author bwplo_000
 */
public class Mob {
    private int ID;
    private int AnimMoveID;
    private int AnimExplodeID;
     private AnimatedObject aob;
     private AnimatedObject aobE;
     public Vector3f pos = new Vector3f(0.0f,0.0f,0.0f);
     private Vector3f rot = new Vector3f(0.0f,0.0f,0.0f);
     private float scale = 0.3f;
     private Color col = new Color(0.0f,0.0f,0.4f,1.0f);
     private MobState state;
    private  Boolean change = true;
     private Vector3f act_check = new Vector3f(0.0f,0.0f,0.0f);
     private Boolean checkpoint_completed = true;
    private  MobState sub_state = MobState.MOVE;
     private float near = 0.5f;
     private float near_rot = 5;
     private float angle_speed = 5; //szybkość zawracania
     private float move_speed = 1; //szybkosc poruszania
     private float angle_mask = 0.0f; //gdzie face
     private  int signX = 1,signZ = 1;
     public int checkpoint = 0;
     public int cost = 10;
     public int value = 3;
     public int to_income = 1;
     public float max_life = 10;
     public int act_check_id = 0;
     public float life=max_life;
     public Vector3f move_dir = new Vector3f(0f,0f,0f);
     public Mob(String type){
       switch(type){
           case "turret1":
               aob = AnimatedObjectBase.o.get(type);
               AnimMoveID = aob.registerObject();
               aobE = AnimatedObjectBase.o.get(type+"ex");
               AnimExplodeID = aobE.registerObject();
               aob.setAnimState(AnimMoveID, AnimationState.STOP, 100);
               aobE.setAnimState(AnimExplodeID, AnimationState.STOP, 50);
               scale = 0.3f;
               near = 1f;
               angle_speed = 15;
               near_rot = angle_speed;
               move_speed = 0.8f;
               angle_mask = 270;
               max_life = 100;
               life=100;
               cost=10;
               value=3;
               break;
       }
       state = MobState.STAND;
      
       
     }
  /*   public void drawRealMode(float x, float z){
            if(checkpoint_completed)
            {
                act_check.x = x;
                act_check.z = z;
                act_check_id++;
                checkpoint_completed = false;
            }
            MobState tmp_state = MobState.MOVE;
            if((act_check.x >= pos.x - near)&&(act_check.x <= pos.x + near))
                if((act_check.z >= pos.z - near)&&(act_check.z <= pos.z + near)){
                    tmp_state = MobState.STAND;
                    checkpoint_completed = true;

                }
            if(tmp_state != state ){
                state = tmp_state; 
                if(state == MobState.MOVE)
                   { 
                       state = MobState.MOVE;
                       sub_state = MobState.ROTATE;
                       aob.setAnimState(AnimMoveID,AnimationState.PLAY);
                   }else{
                        aob.setAnimState(AnimMoveID,AnimationState.PAUSE);
                   }
            }
             aob.setColor(col.R, col.G, col.B, col.A);
            aob.move(pos.x,pos.y,pos.z);
            aob.setRotateY(rot.y);
            aob.setScale(scale);
            if(aob.drawAnimated(AnimMoveID)){
                if(state == MobState.MOVE){
                    
                    if(sub_state == MobState.ROTATE)
                    {
                        float dx =  pos.x - act_check.x;
                        float dz =  pos.z - act_check.z;
                        signX = 1;
                        signZ = 1;
                        if(dx>=0) signX = -1;
                        if(dz>=0) signZ = -1;
                        float dir = (float)(Math.atan(Math.abs(dz)/Math.abs(dx))*180/(Math.PI));
                        if(dx>=0){
                           if(dz>=0){
                               dir *= -1;
                                dir+=180;
                           } else{
                                dir+=180;
                           }
                        }
                        else{
                            if(dz>=0){   
                           }else{
                               dir *= -1;
                               dir+=360;
                          }
                        }
                        
                        dir-=angle_mask;
                        if(dir>360) dir-=360;
                        else if(dir<0) dir+=360;
                        
                        float sign = 1;
                        if(rot.y > 360){
                            rot.y -= 360;
                        }
                        else{
                            if(rot.y < 0){
                            rot.y += 360;
                            }
                        }
                        if(dir < rot.y - angle_speed){
                          

                        }
                        else{
                            if(dir > rot.y + angle_speed){
                              

                           } 
                            else{
                                 rot.y = dir;
                                 sub_state = MobState.MOVE;
                            }

                        }

                        if(sub_state == MobState.ROTATE){
                            if(dir-rot.y > 180 || dir-rot.y< 0) sign = -1;
                           rot.y+= sign*angle_speed;


                        }
                         
                       // aob.setRotateY(dir);
                      //   sub_state = MobState.MOVE; 
                    }
                    else{
                        if((act_check.x >= pos.x - near)&&(act_check.x <= pos.x + near)) signX = 0;
                        if((act_check.z >= pos.z - near)&&(act_check.z <= pos.z + near)) signZ = 0;
                        pos.x += signX*move_speed; pos.z += signZ*move_speed;
                        
                    }

                }
            }
            
         
     }*/
      public void drawRealMode(Vector3f checkPoint,boolean withHp){
            if(checkpoint_completed)
            {
                move_dir.x = checkPoint.x - act_check.x;
                move_dir.z = checkPoint.z - act_check.z;
                float lenC = (float)Math.sqrt(move_dir.x*move_dir.x + move_dir.z*move_dir.z);
                float tmp = move_speed/lenC;
                move_dir.x = move_dir.x*tmp;
                move_dir.z = move_dir.z*tmp;
                act_check = new Vector3f(checkPoint.x,checkPoint.y,checkPoint.z);
                act_check_id++;
                checkpoint_completed = false;
            }
            if(getState() == MobState.EXPLODE){
                 aobE.setColor(col.R, col.G, col.B, col.A);
                 aobE.move(pos.x,pos.y,pos.z);
                 aobE.setRotateY(rot.y);
                 aobE.setScale(scale);
                 aobE.drawAnimated(AnimExplodeID);
                 if(aobE.getLoopNumber(AnimExplodeID)>=1){
                     //kill
                     this.life = 100;
                     aobE.setAnimState(AnimExplodeID, AnimationState.PAUSE);
                 }
                 return;
            }
            
            MobState tmp_state = MobState.MOVE;
            if((act_check.x >= pos.x - near)&&(act_check.x <= pos.x + near))
                if((act_check.z >= pos.z - near)&&(act_check.z <= pos.z + near)){
                    tmp_state = MobState.STAND;
                    checkpoint_completed = true;

                }
            if(tmp_state != getState() ){
                setState(tmp_state); 
                if(getState() == MobState.MOVE)
                   { 
                       setState(MobState.MOVE);
                       sub_state = MobState.ROTATE;
                       aob.setAnimState(AnimMoveID,AnimationState.PLAY);
                   }else{
                        aob.setAnimState(AnimMoveID,AnimationState.PAUSE);
                   }
            }
            aob.setColor(col.R, col.G, col.B, col.A);
            aob.move(pos.x,pos.y,pos.z);
            aob.setRotateY(rot.y);
            aob.setScale(scale);
            if(aob.drawAnimated(AnimMoveID)){
                if(getState() == MobState.MOVE){
                    
                    if(sub_state == MobState.ROTATE)
                    {
                        float dx =  pos.x - act_check.x;
                        float dz =  pos.z - act_check.z;
                        signX = 1;
                        signZ = 1;
                       
                        float dir = (float)(Math.atan(Math.abs(dz)/Math.abs(dx))*180/(Math.PI));
                        if(dx>=0){
                           if(dz>=0){
                               dir *= -1;
                                dir+=180;
                           } else{
                                dir+=180;
                           }
                        }
                        else{
                            if(dz>=0){   
                           }else{
                               dir *= -1;
                               dir+=360;
                          }
                        }
                        
                        dir-=angle_mask;
                        if(dir>360) dir-=360;
                        else if(dir<0) dir+=360;
                        
                        float sign = 1;
                        if(rot.y > 360){
                            rot.y -= 360;
                        }
                        else{
                            if(rot.y < 0){
                            rot.y += 360;
                            }
                        }
                        if(dir < rot.y - angle_speed){
                          

                        }
                        else{
                            if(dir > rot.y + angle_speed){
                              

                           } 
                            else{
                                 rot.y = dir;
                                 sub_state = MobState.MOVE;
                            }

                        }

                        if(sub_state == MobState.ROTATE){
                            if(dir-rot.y > 180 || dir-rot.y< 0) sign = -1;
                           rot.y+= sign*angle_speed;


                        }
                         
                       // aob.setRotateY(dir);
                      //   sub_state = MobState.MOVE; 
                    }
                    else{
                        if((act_check.x >= pos.x - near)&&(act_check.x <= pos.x + near)) signX = 0;
                        if((act_check.z >= pos.z - near)&&(act_check.z <= pos.z + near)) signZ = 0;
                        pos.x += move_dir.x*signX; pos.z += move_dir.z*signZ;
                        
                    }

                }
            }
            if(withHp) drawHP();
     }
      public void drawHP(){
          float v=-life/max_life*2+1;
          
          GL11.glDisable(GL11.GL_LIGHTING);
          GL11.glLineWidth(3f);
          GL11.glTranslatef(pos.x, pos.y, pos.z);
          Graphic.e.rotY(-Camera.bdegree*180f/3.1415f);
          Graphic.e.setColor(new Color(1,0,0,1));
          GL11.glBegin(GL11.GL_LINES);
                GL11.glVertex3f(0, 2f, -1f);
                GL11.glVertex3f(0, 2f, v);
                Graphic.e.setColor(new Color(0,1,0,1));
                GL11.glVertex3f(0, 2f, v);
                GL11.glVertex3f(0, 2f, 1f);
          GL11.glEnd();
          Graphic.e.rotY(Camera.bdegree*180f/3.1415f);
          GL11.glTranslatef(-pos.x, -pos.y, -pos.z);
          
          GL11.glEnable(GL11.GL_LIGHTING);
          
      }
      public void drawCheckpointMode(float x, float z){
           if(checkpoint_completed)
            {
                act_check.x = x;
                act_check.z = z;
                act_check_id++;
                checkpoint_completed = false;
            }
            MobState tmp_state = MobState.MOVE;
            if((act_check.x >= pos.x - near)&&(act_check.x <= pos.x + near))
                if((act_check.z >= pos.z - near)&&(act_check.z <= pos.z + near)){
                    tmp_state = MobState.STAND;
                    checkpoint_completed = true;

                }
            if(tmp_state != getState() ){
                setState(tmp_state); 
                if(getState() == MobState.MOVE)
                   { 
                       setState(MobState.MOVE);
                       sub_state = MobState.ROTATE;
                       aob.setAnimState(AnimMoveID,AnimationState.PLAY);
                   }else{
                        aob.setAnimState(AnimMoveID,AnimationState.PAUSE);
                   }
            }
             aob.setColor(col.R, col.G, col.B, col.A);
             aob.setColor(col.R, col.G, col.B, col.A);
            aob.move(pos.x,pos.y,pos.z);
            aob.setRotateY(rot.y);
            aob.setScale(scale);
            if(aob.drawAnimated(AnimMoveID)){
                if(getState() == MobState.MOVE){
                    
                    if(sub_state == MobState.ROTATE)
                    {
                        float dx =  pos.x - act_check.x;
                        float dz =  pos.z - act_check.z;
                        signX = 1;
                        signZ = 1;
                        if(dx>=0) signX = -1;
                        if(dz>=0) signZ = -1;
                        float dir = (float)(Math.atan(Math.abs(dz)/Math.abs(dx))*180/(Math.PI));
                        if(dx>=0){
                           if(dz>=0){
                               dir *= -1;
                                dir+=180;
                           } else{
                                dir+=180;
                           }
                        }
                        else{
                            if(dz>=0){   
                           }else{
                               dir *= -1;
                               dir+=360;
                          }
                        }
                        
                        dir-=angle_mask;
                        if(dir>360) dir-=360;
                        else if(dir<0) dir+=360;  
                       rot.y = dir;
                      sub_state = MobState.MOVE; 
                    }
                    else{
                        if((act_check.x >= pos.x - near)&&(act_check.x <= pos.x + near)) signX = 0;
                        if((act_check.z >= pos.z - near)&&(act_check.z <= pos.z + near)) signZ = 0;
                        pos.x += signX*move_speed; pos.z += signZ*move_speed;
                        
                    }

                }
            }
            
         
     }

    /**
     * @return the ID
     */
    public int getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(int ID) {
        this.ID = ID;
    }

    /**
     * @return the state
     */
    public MobState getState() {
        return state;
    }

    /**
     * @param state the state to set
     */
    public void setState(MobState state) {
        if(state == MobState.EXPLODE){
            aobE.setAnimState(AnimExplodeID, AnimationState.PLAY);
            this.state = state;
        }
        
        if(this.state != MobState.EXPLODE){
            this.state = state;
        }
    }
}
